import { fileURLToPath, URL } from "node:url";

import { defineConfig } from "vite";
import legacy from "@vitejs/plugin-legacy";
import vue2 from "@vitejs/plugin-vue2";
import { visualizer } from "rollup-plugin-visualizer";
import process from "process";

// https://vitejs.dev/config/
const isLegacy = process.env.LEGACY === "true";

const plugins = [
  vue2(),
  visualizer({
    open: true,
    gzipSize: true,
    brotliSize: true,
    filename: "stats.html",
  }),
];
if (isLegacy) {
  plugins.push(
    legacy({
      targets: ["ie >= 11"],
      additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
    })
  );
}

export default defineConfig({
  plugins,
  resolve: {
    alias: {
      process: "process/browser",
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
    extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
  },
  server: {
    host: "0.0.0.0",
    port: 8080,
    proxy: {
      "/api": {
        target: "http://",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ""),
      },
    },
  },
});
